package com.qp.basics.estivate.support;

import com.qp.basics.estivate.DbHelper;
import com.qp.basics.estivate.IgnoreCaseMap;
import com.qp.basics.estivate.ResultSetExtractor;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

/**
 * 将一条记录以字段名为key存到一个忽略key的大小写的Map里。
 */
public class MapResultSetExtractor implements ResultSetExtractor {

	public Object extractData(ResultSet resultset) {
		if (resultset == null) {
			return null;
		}
		Map rsMap = null;
		int columnCount;
		try {
			java.sql.ResultSetMetaData metaData = resultset.getMetaData();
			columnCount = metaData.getColumnCount();
			rsMap = new IgnoreCaseMap(columnCount);
			for (int i = 0; i < columnCount; i++) {
				String colName = metaData.getColumnLabel(i + 1);
				Object o = DbHelper.getResultSetValue(resultset, i + 1);
				rsMap.put(colName, o);
			}
		} catch (SQLException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
		return rsMap;
	}

}
